﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wpf.Ui.Controls;
using Wpf.Ui;
using ClientAppraise.Controls;
using System.Drawing;
using System.Threading;
using System.Windows;
using ClientInfrastructure.Dto;

namespace ClientAppraise.Services
{
    public partial class ZxNavigationService : IZxNavigationService
    {
        /// <summary>
        /// Locally attached service provider.
        /// </summary>
        private readonly IServiceProvider _serviceProvider;

        /// <summary>
        /// Locally attached page service.
        /// </summary>
        private IPageService? _pageService;

        /// <summary>
        /// Control representing navigation.
        /// </summary>
        protected IZxNavigationView? NavigationControl;

        /// <summary>
        /// Initializes a new instance of the <see cref="NavigationService"/> class.
        /// </summary>
        /// <param name="serviceProvider">Service provider providing page instances.</param>
        public ZxNavigationService(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        /// <inheritdoc />
        public IZxNavigationView GetNavigationControl()
        {
            return NavigationControl ?? throw new ArgumentNullException(nameof(NavigationControl));
        }

        /// <inheritdoc />
        public void SetNavigationControl(IZxNavigationView navigation)
        {
            NavigationControl = navigation;

            if (_pageService != null)
            {
                NavigationControl.SetPageService(_pageService);

                return;
            }

            NavigationControl.SetServiceProvider(_serviceProvider);
        }

        /// <inheritdoc />
        public void SetPageService(IPageService pageService)
        {

            if (NavigationControl == null)
            {
                _pageService = pageService;

                return;
            }

            ThrowIfPageServiceIsNull();

            NavigationControl.SetPageService(_pageService!);
        }

        /// <inheritdoc />
        public bool Navigate(Type pageType)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.Invoke(new Action(() => Navigate(pageType)));
                return true;
            }
            ThrowIfNavigationControlIsNull();

            return NavigationControl!.Navigate(pageType);
        }

        /// <inheritdoc />
        public bool Navigate(string pageTag)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.Invoke(new Action(() => Navigate(pageTag)));
                return true;
            }
            ThrowIfNavigationControlIsNull();

            return NavigationControl!.Navigate(pageTag);
        }

        /// <inheritdoc />
        public bool GoBack()
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.Invoke(new Action(() => GoBack()));
                return true;
            }
            ThrowIfNavigationControlIsNull();

            return NavigationControl!.GoBack();
        }
        public bool NavigateWithHierarchy(Type pageType) {
           return NavigateWithHierarchy(pageType, null);
        }
        /// <inheritdoc />
        public bool NavigateWithHierarchy(Type pageType, PageParameterDto? parameterDto)
        {
            if (!Application.Current.Dispatcher.CheckAccess())
            {
                Application.Current.Dispatcher.Invoke(new Action(() => NavigateWithHierarchy(pageType, parameterDto)));
                return true;
            }
            ThrowIfNavigationControlIsNull();

            return NavigationControl!.NavigateWithHierarchy(pageType, parameterDto);
        }

        private void ThrowIfNavigationControlIsNull()
        {
            if (NavigationControl is null)
            {
                throw new ArgumentNullException(nameof(NavigationControl));
            }
        }

        private void ThrowIfPageServiceIsNull()
        {
            if (_pageService is null)
            {
                throw new ArgumentNullException(nameof(_pageService));
            }
        }
    }
}
